package com.springboot.datasource.config;

import com.springboot.datasource.dto.DataSourceDto;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname LoadDataSourceRunner
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/1/17 9:59
 * @Created by zyj
 */
@Component
public class LoadDataSourceRunner implements CommandLineRunner {

    @Resource
    private DynamicDataSource dynamicDataSource;

    @Override
    public void run(String... args) throws Exception {
        List<DataSourceDto> ds = initDataSource();
        dynamicDataSource.createDataSource(ds);
    }

    public List<DataSourceDto> initDataSource(){
        List<DataSourceDto> ds = new ArrayList<>();
        DataSourceDto ds1 = new DataSourceDto();
        ds1.setKey("ds1");
        ds1.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds1.setPassword("123456");
        ds1.setUsername("root");
        ds1.setUrl("jdbc:mysql://192.168.18.2:3306/user?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8");
        ds.add(ds1);


        DataSourceDto ds2 = new DataSourceDto();
        ds2.setKey("ds2");
        ds2.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds2.setPassword("123456");
        ds2.setUsername("root");
        ds2.setUrl("jdbc:mysql://192.168.18.2:3306/tx_manager?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8");
        ds.add(ds2);

        DataSourceDto ds3 = new DataSourceDto();
        ds3.setKey("ds3");
        ds3.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds3.setPassword("123456");
        ds3.setUsername("root");
        ds3.setUrl("jdbc:mysql://192.168.18.2:3306/testuser?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8");
        ds.add(ds3);

        DataSourceDto ds4 = new DataSourceDto();
        ds4.setKey("ds4");
        ds4.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds4.setPassword("123456");
        ds4.setUsername("root");
        ds4.setUrl("jdbc:mysql://192.168.18.2:3306/dcfq?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8");
        ds.add(ds4);

        return ds;
    }
}
